package com.eric.windows.viewAnimation;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eric.windows.R;

import java.util.ArrayList;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/1 11:31
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */
public class GridBaseAdapter extends BaseAdapter {
    private int screenNo;
    private Context context;
    private ArrayList<DataItem> items;
    /** 0 viewSwitcher测试 1 ImageSwitcher 测试 */
    private  int  type = 0;
    /** 屏幕总数*/
    private int screenCount = 0;
    /** 屏幕的宽度*/
    private int ScreenWith;
    private LinearLayout.LayoutParams layoutParams;

    public GridBaseAdapter(int screenNo, Context context, ArrayList<DataItem> items,int screenCount) {
        this(screenNo,context,items,screenCount,0);
    }

    public GridBaseAdapter(Context context, ArrayList<DataItem> items,int type) {
        this(0,context,items,0,type);
    }


    public GridBaseAdapter(int screenNo, Context context, ArrayList<DataItem> items,int screenCount,int type) {
        this.screenNo = screenNo;
        this.context = context;
        this.items = items;
        this.screenCount = screenCount;
        this.type = type;
        this.ScreenWith = getScreenWith();
        if (type == 1){
            this.ScreenWith = this.ScreenWith/4;
            layoutParams = new LinearLayout.LayoutParams(this.ScreenWith,this.ScreenWith);
        }else {
            this.ScreenWith = this.ScreenWith/6;
            layoutParams = new LinearLayout.LayoutParams(this.ScreenWith,this.ScreenWith);
        }
    }


    @Override
    public int getCount() {
        if (type == 1){
            return items == null ? 0 : items.size();
        }else if (type == 0){
            if (items == null){
                return  0;//到最后一屏，且不能整除
            }else if (items.size() % ViewSwitcherActivity.NUMBER_PER_SCREEN != 0
                    &&  screenNo == screenCount -1 ){
                return items.size() % ViewSwitcherActivity.NUMBER_PER_SCREEN;
            }else {// 否则返回 NUMBER_PER_SCREEN
                return ViewSwitcherActivity.NUMBER_PER_SCREEN;
            }
        }else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_grid,parent,false);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.item_img_vs);
            holder.txt = (TextView) convertView.findViewById(R.id.item_txt_vs);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (type == 1){
            holder.txt.setVisibility(View.GONE);
        }

        holder.img.setImageDrawable(items.get(position).drawable);
        holder.img.setLayoutParams(layoutParams);
        holder.img.setScaleType(ImageView.ScaleType.FIT_XY);


        if (!TextUtils.isEmpty(items.get(position).dataName)){
            holder.txt.setText(items.get(position).dataName);
        }

        return convertView;
    }


    static class ViewHolder{
        public ImageView img;
        public TextView txt;
    }

    /**
     * 获取屏幕宽度
     * @return
     */

    private int getScreenWith(){
        Display display  = ((Activity)context).getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        return metrics.widthPixels;
    }

}
